package config

import (
	"io/ioutil"
	"os"

	log "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"
)

type ymalCfg struct {
	SvrName   string   `yaml:"SvrName"`
	SvrPort   uint32   `yaml:"SvrPort"`
	SvrIp     string   `yaml:"SvrIp"`
	PortRange uint32   `yaml:"PortRange"`
	Redis     []string `yaml:"Redis"`
	Fronter   []string `yaml:"Fronter"`
}

var svrCfg ymalCfg
var cfgContent []byte

func Get() ymalCfg {
	return svrCfg
}

func init() {
	var err error
	const filename = "./svr.conf"
	if cfgContent, err = ioutil.ReadFile(filename); err != nil {
		log.Errorln("open svr conf fail! please check!")
		os.Exit(-1)
	} else {
		log.Printf("cfg: %s\n", cfgContent)

	}
	parseYaml()
}

func parseYaml() {
	err := yaml.Unmarshal(cfgContent, &svrCfg)
	if err != nil {
		log.Errorln("prase yaml config failed! please check!")
		os.Exit(-1)
	}
}
